package com.imooc.service.impl;

import com.imooc.enums.YesOrNo;
import com.imooc.mapper.UserAddressMapper;
import com.imooc.pojo.UserAddress;
import com.imooc.pojo.bo.AddressBO;
import com.imooc.service.AddressService;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private Sid sid;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<UserAddress> addressList(String userId) {

        Example example=new Example(UserAddress.class);
        Example.Criteria criteria=example.createCriteria();

        criteria.andEqualTo("userId",userId);

        List<UserAddress> list=userAddressMapper.selectByExample(example);

        return list;
    }

    @Override
    public void userAddressUpdate(String userId, String addressId) {
        //1.查找默认地址，设置为不默认
        Example example=new Example(UserAddress.class);
        Example.Criteria criteria=example.createCriteria();

        criteria.andEqualTo("userId",userId);

        List<UserAddress> list=userAddressMapper.selectByExample(example);
        for (UserAddress ua:list){
            ua.setIsDefault(YesOrNo.NO.type);
            userAddressMapper.updateByPrimaryKeySelective(ua);
        }
        //2.设置默认地址
        UserAddress address=new UserAddress();
        address.setUserId(userId);
        address.setId(addressId);
        address.setIsDefault(YesOrNo.YES.type);
        userAddressMapper.updateByPrimaryKeySelective(address);

    }

    @Override
    public void userAddressCreate(AddressBO addressBO) {
        UserAddress address=new UserAddress();
        String addressId=sid.nextShort();
        address.setId(addressId);
        address.setIsDefault(YesOrNo.NO.type);
        address.setUserId(addressBO.getUserId());
        address.setCity(addressBO.getCity());
        address.setCreatedTime(new Date());
        address.setDetail(addressBO.getDetail());
        address.setDistrict(addressBO.getDistrict());
        address.setMobile(addressBO.getMobile());
        address.setProvince(addressBO.getProvince());
        address.setReceiver(addressBO.getReceiver());
        address.setUpdatedTime(new Date());

        userAddressMapper.insertSelective(address);
    }

    @Override
    public void userAddressUpdate(AddressBO addressBO) {
        UserAddress address=new UserAddress();
        address.setId(addressBO.getAddressId());
        address.setUserId(addressBO.getUserId());
        address.setCity(addressBO.getCity());
        address.setDetail(addressBO.getDetail());
        address.setDistrict(addressBO.getDistrict());
        address.setMobile(addressBO.getMobile());
        address.setProvince(addressBO.getProvince());
        address.setReceiver(addressBO.getReceiver());
        address.setUpdatedTime(new Date());

        userAddressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public UserAddress queryUserAddres(String userId, String addressId) {

        UserAddress address=new UserAddress();
        address.setId(addressId);
        address.setUserId(userId);
        return userAddressMapper.selectOne(address);
    }
}
